package Work.Stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class TestStream {
    public static void main(String[] args) {
        List<Book> bookList = Arrays.asList(
                new Book("四大名著",288),
                new Book("十万个为什么",99),
                new Book("三体全册",368),
                new Book("流浪地球",88),
                new Book("凡尔纳科幻三部曲",178)
        );

        System.out.println("---图书列表---");
        List<String> booknames1 = new ArrayList<>();
        Iterator<Book> iterator = bookList.iterator();
        while (iterator.hasNext()) {
            Book b = iterator.next();
            booknames1.add(b.toString());
        }
        booknames1.forEach(x -> System.out.println(x));

        System.out.println("---价格大于150块的图书---");
        List<String> booknames2 = bookList.stream()
                .filter(x -> x.getPrice()>150)
                .sorted((x1,x2) -> x1.getPrice()- x2.getPrice())
                .map(x -> x.toString())
                .collect(Collectors.toList());
        booknames2.forEach(x -> System.out.println(x));
    }
}
